﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace ZWZ.Web
{
    public static class ControlExt
    {
        public static void BindEnumDataSource(this DropDownList list, Type em_type, bool ValueIsText = true)
        {
            var em_values = Enum.GetValues(em_type);
            list.Items.Clear();
            foreach (int em_value in em_values)
            {
                string name = Enum.GetName(em_type, em_value);
                var item = new ListItem();
                item.Text = name;
                item.Value = ValueIsText ? name : em_value.ToString();
                list.Items.Add(item);
            }
        }
        public static string TryGetText(this TextBox txt, string FieldName)
        {
            if (string.IsNullOrEmpty(txt.Text))
            {
                throw new Exception("请输入" + FieldName);
            }
            return txt.Text;
        }
        public static DateTime TryGetDate(this TextBox txt, string FieldName,bool IsRequird=true)
        {
            DateTime result;
            if (!DateTime.TryParse(txt.Text, out result)&&IsRequird)
            {
                throw new Exception("请输入有效的"+FieldName);
            }
            return result;
        }
    }
}